/**
 * Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 *
 * For the applicable distribution terms see the license.txt -file, included in
 * the distribution.
 */

#include "audioplayer.h"
#include "GEAudio/GEAudioMixer.h"
#include "GEAudio/GEAudioBuffer.h"

#ifdef Q_OS_SYMBIAN
#include <QTimer>
#endif

AudioPlayer::AudioPlayer(QObject *parent) :
    QObject(parent),
    m_audioOut(0),
    m_mixer(0),
    m_playInstance(0),
    m_silent(false),
    m_playing(false)
{
    m_mixer = new GE::AudioMixer(this);
    m_audioOut = new GE::AudioOut(m_mixer, this);
    GE::AudioBuffer *buffer = GE::AudioBuffer::loadWav(":/sounds/motor2.wav", this);
    m_playInstance = buffer->playWithMixer(*m_mixer);
    m_playInstance->setLoopCount(-1);
    m_playInstance->setLeftVolume(0);
    m_playInstance->setRightVolume(0);

#ifdef Q_OS_SYMBIAN
    QTimer *timer = new QTimer(this);
    QObject::connect(timer, SIGNAL(timeout()), m_audioOut, SLOT(tick()));
    timer->start(5);
#endif
}

void AudioPlayer::setSpeed(qreal speed)
{
    m_playInstance->setSpeed((float)speed);
}

void AudioPlayer::stop()
{
    m_playing = false;
    m_playInstance->setLeftVolume(0);
    m_playInstance->setRightVolume(0);
}

void AudioPlayer::start()
{
    m_playing = true;

    if (!m_silent) {
        m_playInstance->setLeftVolume(1);
        m_playInstance->setRightVolume(1);
    }
}
